﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ueEngine.Biz.Inf.LogicEntity;
using ueEngine.Biz.Inf.Entities.DBSupport;
using ueEngine.Framework.Common;
using ueEngine.Biz.Inf.Entities;

namespace ueEngine.Biz.Core.AppService.Support
{
    public class SystemService
    {
        public ItemListBox<NoticeModel> BaseNotice(int pagesize)
        {
            using (Context ctx = new Context())
            {
                var list = ctx.SystemNotice.OrderByDescending(c => c.Id).Take(pagesize)
                    .ToList()
                    .Select(c=>new NoticeModel(){
                        Content =c.Content
                    })
                    .ToList();
                if (list != null && list.Any())
                {
                    return new ItemListBox<NoticeModel>(list);
                }
                return null;
            }
        }
        #region DiscussRegion
        public bool PostDiscuss(ShortDiscussModel model)
        {
            if (model != null && model.PostAdd() != null)
            {
                return true;
            }
            return false;
        }
        public void DiscussFeedback(int discussid, bool isUp)
        {
            using (Context ctx = new Context())
            {
                var item = new ShortDiscussModel().Element(c => c.Id == discussid);
                if (isUp)
                {
                    item.Up += 1;
                }
                else
                {
                    item.Down += 1;
                }
                item.PostModify();
            }
        }
        public ItemListBox<ShortDiscussModel> QueryOfDiscuss(int page, int pageSize, ReferenceCode code, int referenceId, int apparise)
        {
            using(Context ctx = new Context())
            {
                var query = from c in ctx.ShortDiscuss
                            where c.ReferenceCode == (int)code && c.ReferenceId == referenceId && !c.IsDelete
                            select c;
                if (apparise >= 0 && apparise <= 2)
                {
                    query = query.Where(c => c.Appraise == apparise);
                }
                int itemCount = query.Count();
                var list =query
                    .OrderByDescending(c=>c.WriteDate)
                    .Skip(page * pageSize)
                    .Take(pageSize)
                    .ToList()
                    .Select(c=> new ShortDiscussModel(){
                        Author=c.Author,
                        Content=c.Content,
                        Id=c.Id,
                        ReferenceCode= (ReferenceCode)c.ReferenceCode,
                        ReferenceId=c.ReferenceId,
                        WriteDate=c.WriteDate,
                        Appraise = (Appraise)c.Appraise,
                        Up = c.Up,
                        Down = c.Down
                    })
                    .ToList();
                return new ItemListBox<ShortDiscussModel>(list)
                    .BuildPage(itemCount,page,pageSize,new PageParameter(){
                        IsActive=false
                    }) ;
            }
             //new ShortDiscussModel().Elements(c => c.ReferenceCode == (int)code && c.ReferenceId == referenceId);
        }
        public int GetDiscussUpOrDown(int discussId, bool isUp)
        {
            using (Context ctx = new Context())
            {
                if (isUp)
                {
                    return ctx.ShortDiscuss.Where(c => c.Id == discussId).Select(c => c.Up).FirstOrDefault();
                }
                else
                {
                    return ctx.ShortDiscuss.Where(c => c.Id == discussId).Select(c => c.Down).FirstOrDefault();
                }
            }
        }
        internal void UpdateDiscussUpOrDown(int discussId, int changeVal, bool isUp)
        {
            using (Context ctx = new Context())
            {
                var item = new ShortDiscussModel().Element(c => c.Id == discussId);
                if (isUp)
                {
                    item.Up += changeVal;
                }
                else
                {
                    item.Down += changeVal;
                }
                var res = item.PostModify();
                if (res != null)
                {
                    LogHelper.Write(CommonLogger.Cache, LogLevel.Trace,
                        String.Format("执行UpdateDiscussUpOrDown_id:{0}_changeVal:{1}_isUp:{2}_resultUp:{3}_resultDown:{4}",
                        discussId, changeVal, isUp, res.Up, res.Down));
                }
                else
                {
                    LogHelper.Write(CommonLogger.Cache, LogLevel.Trace,
                        String.Format("执行UpdateDiscussUpOrDown_id:{0}_changeVal:{1}_isUp:{2}_Faild",
                        discussId, changeVal, isUp));
                }
            }
        }
        /// <summary>
        /// 获取对企业用户的评论概况
        /// </summary>
        /// <param name="username"></param>
        /// <returns>好评次数：中评次数：差评次数</returns>
        public string GetDiscussForAgentOfAppraiseDetail(string username)
        {
            var user = new ueEngine.Biz.Core.AppService.Member.UserService(username).GetUser();
            if (user == null || user.Role != MemberStatus.Firm)
            {
                return "0:0:0";
            }

            using (Context ctx = new Context())
            {
                var query = (from c in ctx.ShortDiscuss
                             where c.ReferenceCode == (int)ReferenceCode.Agent &&
                                   c.ReferenceId == user.Id && !c.IsDelete
                             select c.Appraise);
                var res = query.ToList();
                return res.Count(c => c == 1).ToString() + ":" + res.Count(c => c == 0).ToString() + ":" + res.Count(c => c == 2).ToString() + ":" + res.Count;
            }
        }
        public int QueryOfDiscussTimeForRecruitment(int recruitid)
        {
            using (Context ctx = new Context())
            {
                return ctx.ShortDiscuss.Count(
                    c =>
                        c.ReferenceCode == (int)ReferenceCode.Recruitment &&
                        c.ReferenceId == recruitid && !c.IsDelete);
            }
        }
        public string GetDiscussForRecruitmentOfAppraiseDetail(int id)
        {
            using (Context ctx = new Context())
            {
                var query = (from c in ctx.ShortDiscuss
                             where c.ReferenceCode == (int)ReferenceCode.Recruitment &&
                                   c.ReferenceId == id && !c.IsDelete
                             select c.Appraise);
                var res = query.ToList();
                return res.Count(c => c == 1).ToString() + ":" + res.Count(c => c == 0).ToString() + ":" + res.Count(c => c == 2).ToString() + ":" + res.Count;
            }
        }
        #endregion

        #region FeedbackRegion
        public bool PostFeedback(FeedbackModel model)
        {
            if (model != null && model.PostAdd() != null)
            {
                return true;
            }
            return false;
        }
        public ItemListBox<FeedbackModel> QueryOfFeedback(int page,int pageSize,FeedbackType type)
        {
            using (Context ctx = new Context())
            {
                var query = from c in ctx.Feedback
                            where c.FeedbackType == (int)type
                            select c;
                int itemCount = query.Count();
                var list = query.ToList()
                    .Select(c => new FeedbackModel()
                    {
                        Admin = c.Admin,
                        FeedbackType = (Inf.LogicEntity.FeedbackType)c.FeedbackType,
                        Id = c.Id,
                        IsProcess = c.IsProcess,
                        Message = c.Message,
                        ReferenceUri = c.ReferenceUri,
                        User = c.User,
                        WriteDate = c.WriteDate
                    }).ToList();
                return new ItemListBox<FeedbackModel>(list).BuildPage(itemCount, page, pageSize);
            }
        }
        #endregion
    }
}
